import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import router from '@/router'
import notification from 'ant-design-vue/es/notification'
import { VueAxios } from './axios'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { encrypt } from '@/utils/encrypt'
const service = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL,
  timeout: 300000
})

const err = (error) => {
  if (error.response) {
    const data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    if (error.response.status === 403) {
      notification.error({
        message: 'Forbidden',
        description: data.message
      })
    }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      notification.error({
        message: 'Unauthorized',
        description: 'Authorization verification failed'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
  }
  return Promise.reject(error)
}

service.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers['accessToken'] = token 
  }
  if (config.data != '[object FormData]' && config.headers.post['Content-Type'] == 'application/x-www-form-urlencoded') {
    // console.log(config.data);
    
    // config.data = config.data
    config.data = {'text': encrypt(config.data)}
  }
  return config
}, err)

service.interceptors.response.use((response) => {
  const res = response.data;
  if (res.code === '1' || res.code == undefined) {
      return res;
   } else if (res.code === '0') {
     notification.error({
        message: '温馨提示',
        description: '网络不稳定，刷新页面重试或联系管理员！'
      })
      return res;
   } else if (res.code === '106') {
       notification.error({
        message: '温馨提示',
        description: res.message
       })
       store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      return Promise.reject();
  } else if (res.code === '104') {
       notification.error({
        message: '温馨提示',
        description: '登录超时，请重新登录'
       })
       store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      return Promise.reject();
  } else if (res.code === '111') {
       notification.error({
        message: '温馨提示',
        description: res.message
       })
       store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      return Promise.reject();
  } else {
      notification.error({
        message: '温馨提示',
        description: res.message
      })
      return res;
    }
}, err => {
  var json = JSON.stringify(err.response);
  var errorState = JSON.parse(json);
  if (errorState.status === 302) { 
    localStorage.clear();
    router.push({ path: '/error' });
    return Promise.reject(errorState.statusText);
  }
})
const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, service)
  }
}

export {
  installer as VueAxios,
  service as axios
}
